package com.emex.app.stuff.gui.model;

import com.emex.app.shared.data.Cost;
import com.emex.app.shared.data.CostType;
import com.emex.app.shared.data.enums.PayMethod;
import com.emex.app.stuff.provider.CostDataProvider;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Sergey Karavaev
 */
public class CostTableModel extends AbstractTableModel
        implements TableModel, TableCrud<Cost> {

    private Double sum = 0.0;
    /**
     * Cache of cost types to display in table.
     */
    private final List<CostType> costTypes;
    /**
     * List of all orders.
     */
    private final List<Cost> costs;
    /**
     * Column names.
     */
    protected String[] columnNames = new String[]{
        "Дата", "Статья расхода", "Сумма", "Тип оплаты", "Комментарий"
    };
    /**
     * Column classes
     */
    protected Class[] columnClasses = new Class[]{
        Date.class, String.class, Double.class, PayMethod.class, String.class
    };

    /**
     * {@inheritDoc}
     */
    @Override
    public Class getColumnClass(int index) {
        return columnClasses[index];
    }

    public CostTableModel(List<Cost> listCosts) {
        costs = listCosts;
        costTypes = CostDataProvider.getInstance().getAllCostTypes();
        calculateSums();
    }
    
    private void calculateSums() {
        if (null != costs && !costs.isEmpty()) {
            for (Cost cost: costs) {
                sum += cost.getSumma();
            }
        }
    }

    public Double getSum() {
        return sum;
    }

    public void setSum(Double sum) {
        this.sum = sum;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public int getRowCount() {
        return costs.size();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if ((rowIndex >= 0) && (rowIndex < costs.size())
                && (columnIndex >= -1) && (columnIndex < columnNames.length)) {
            switch (columnIndex) {
                case -1: {
                    return costs.get(rowIndex);
                }
                case 0:
                    return costs.get(rowIndex).getDate();
                case 1: {
                    return getCostTypeById(costs.get(rowIndex).getCostTypeId());
                }
                case 2:
                    return costs.get(rowIndex).getSumma();
                case 3:
                    return PayMethod.valueOf(costs.get(rowIndex).getPayMethod());
                case 4:
                    return costs.get(rowIndex).getComment();
                default:
                    return null;
            }
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public void dataRemoved(Cost t) {
        costs.remove(t);
        this.fireTableDataChanged();
    }

    @Override
    public void dataAdded(Cost t) {
        costs.add(t);
        this.fireTableDataChanged();
    }

    @Override
    public void dataUpdated() {
        this.fireTableDataChanged();
    }

    private String getCostTypeById(Integer id) {
        for (CostType ct : costTypes) {
            if (Objects.equals(ct.getId(), id)) {
                return ct.getName();
            }
        }
        return "";
    }
}
